package com.inspur.cloud.service.dataspace;

import com.inspur.cloud.entity.dataspace.vo.VerifyCodeDTO;
import org.apache.tomcat.util.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

/**
 * 生成验证码服务
 *
 * @author dh 2019-07-20
 */
@Service
public class VerifyCodeService {
    protected static final Logger logger = LoggerFactory.getLogger(VerifyCodeService.class);

    /**
     * 验证码字符
     */
    private static char[] mapTable = {'a', 'b', 'c', 'd', 'e', 'h', 'j', 'k',
            'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
            '0', '2', '3', '4', '5', '6', '7', '8', '9'};

    /**
     * 生成验证码
     *
     * @param width
     * @param height
     * @return
     */
    public VerifyCodeDTO getVerifyCode(int width, int height) throws NoSuchAlgorithmException, IOException {
        BufferedImage image = new BufferedImage(width, height, 1);
        // 产生Image对象的Graphics对象,改对象可以在图像上进行各种绘制操作
        Graphics graphics = image.getGraphics();

        graphics.setColor(Color.WHITE);

        graphics.fillRect(0, 0, width, height);

        graphics.setFont(new Font("STIX", Font.BOLD, 20));

        graphics.setColor(getRandColor(160, 200));

        // 绘制干扰线
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        for (int i = 0; i < 155; i++) {
            int x = random.nextInt(width);
            int y = random.nextInt(height);

            int xl = random.nextInt(12);
            int yl = random.nextInt(12);

            graphics.drawLine(x, y, x + xl, y + yl);
        }

        // 绘制随机字符
        String strEnsure = "";

        for (int i = 0; i < 4; i++) {
            char c = mapTable[((int) (random.nextInt(mapTable.length)))];

            strEnsure = strEnsure + c;

            graphics.setColor(new Color(20 + random.nextInt(110), 20 + random
                    .nextInt(110), 20 + random.nextInt(110)));
            graphics.drawString(String.valueOf(c), 13 * i + 6, 16);
        }

        graphics.dispose();

        // 图片转图片流
        ByteArrayOutputStream imOut = new ByteArrayOutputStream();
        String base64 = null;
        byte[] bytes = null;
        ImageIO.write(image, "jpg", imOut);
        bytes = imOut.toByteArray();
        base64 = Base64.encodeBase64String(bytes);
        return new VerifyCodeDTO(strEnsure, base64, bytes);
    }

    /**
     * 图片颜色随机
     *
     * @param fc
     * @param bc
     * @return
     */
    private Color getRandColor(int fc, int bc) throws NoSuchAlgorithmException {
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        int r = fc + random.nextInt(bc - fc);
        int g = fc + random.nextInt(bc - fc);
        int b = fc + random.nextInt(bc - fc);
        return new Color(r, g, b);
    }
}
